package com.zcxy360.sandtable.controller;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zcxy360.sandtable.service.BusinessModelService;
import com.zcxy360.sandtable.service.DrawingTypeService;
import com.zcxy360.sandtable.service.OrgTypeService;
import com.zcxy360.sandtable.util.MessageUtil;

/**
 * 一般用途的控制器,用来获取通用数据
 * @author 盛亚辉
 *
 */
@RequestMapping("common")
@RestController
public class CommonController {

	@Value("${res.server}")
	private String resServer;
	
	@Autowired
	private BusinessModelService businessModelService;
	@Autowired
	private DrawingTypeService drawingTypeService;
	@Autowired
	private OrgTypeService orgTypeService;

	/**
	 * 获取商业模式集合
	 * @return 商业模式集合
	 */
	@RequiresAuthentication
	@GetMapping("listBusinessModel")
	public Object listBusinessModel() {
		return businessModelService.list();
	}
	
	/**
	 * 获取图纸类型集合
	 * @return 图纸类型集合
	 */
	@RequiresAuthentication
	@GetMapping("listDrawingType")
	public Object listDrawingType() {
		return drawingTypeService.list();
	}
	
	/**
	 * 获取机构类型集合
	 * @return 机构类型集合
	 */
	@RequiresAuthentication
	@GetMapping("listOrgType")
	public Object listOrgType() {
		return orgTypeService.list();
	}
	
	/**
	 * 发送手机验证码
	 * @param phone 手机号
	 * @return 布尔值
	 */
	@GetMapping("sendCode")
	public Object sendCode(@RequestParam("phone")String phone) {
		return MessageUtil.sendCode(phone);
	}
	
	/**
	 * 检查手机验证码
	 * @param phone 手机号
	 * @param code 验证码
	 * @return 布尔值
	 */
	@GetMapping("checkCode")
	public Object checkCode(@RequestParam("phone")String phone,@RequestParam("code")String code) {
		String cacheCode = MessageUtil.CODE_CACHE.get(phone);
		return cacheCode == null?false:cacheCode.equals(code);
	}
	
}
